package com.concerneyes.rentbarsecond;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.concerneyes.R;
import com.concerneyes.mine.Task.OrderImpl;
import com.concerneyes.mine.Task.OrderListener;
import com.concerneyes.mine.model.OrderSubmit;
import com.concerneyes.pay.ConfirmInfoActivity;
import com.concerneyes.pay.DialogPay;
import com.concerneyes.rentbar.RentBarAdapter;
import com.concerneyes.rentbarsecond.adapter.NewRentBarAdapter;
import com.concerneyes.rentbarsecond.db.GlassesDetailModelForDao;
import com.concerneyes.rentbarsecond.db.RentBarDao;

import java.util.HashMap;
import java.util.List;

import common.AbsBaseView;
import util.ActivityUtil;

/**
 * 租Bar
 */

public class RentBarNewView extends AbsBaseView implements View.OnClickListener,
        NewRentBarAdapter.OnPayListener, DialogPay.PaySuccessListener, OrderListener {
    private ListView lv_rentbar;
    private NewRentBarAdapter adapter;
    private Context context;
    private OrderImpl orderImpl;
    private List<GlassesDetailModelForDao> modelForDaos;
    private int orderId;
    private int itemPosition;
    private RelativeLayout rl_bottom_delete;
    private TextView delete_now;
    private LinearLayout ll_choose_all;
    private ImageView iv_choose_all;
    private boolean isItemAllClick = true;
    private boolean isItemClick = true;
    private HashMap<Integer, Boolean> itemSelete = new HashMap<>();

    public RentBarNewView(Context context) {
        super(context);
        this.context = context;
        initView();
    }

    @Override
    public int layoutResId() {
        return R.layout.activity_rentbar_new;
    }

    @Override
    public void loadData(Object data) {
        modelForDaos = new RentBarDao(context).getAllValid();
        adapter.clear();
        adapter.addItems(modelForDaos);
        Log.e("RentBarNewView", "itemSelete.size()" + itemSelete.size());
        if (itemSelete.size() == 0) {
            adapter.setVisible(false);
            rl_bottom_delete.setVisibility(View.GONE);
        }
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    private void initView() {
        lv_rentbar = (ListView) findViewById(R.id.lv_rentbar);
        delete_now = (TextView) findViewById(R.id.delete_now);
        rl_bottom_delete = (RelativeLayout) findViewById(R.id.rl_bottom_delete);
        ll_choose_all = (LinearLayout) findViewById(R.id.ll_choose_all);
        iv_choose_all = (ImageView) findViewById(R.id.iv_choose_all);
        adapter = new NewRentBarAdapter(getContext(), this, new RentBarDao(context).getAllValid());
        lv_rentbar.setAdapter(adapter);
        orderImpl = new OrderImpl(this);
        ll_choose_all.setOnClickListener(this);
        delete_now.setOnClickListener(this);
        loadData(null);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.delete_now:
                boolean isTrue = false;
                Log.e(this.getClass().getSimpleName(), "itemSeletemap==>" + itemSelete.toString());
                if (itemSelete != null && itemSelete.size() > 0) {
                    for (int i = 0; i < itemSelete.size(); i++) {
                        if (itemSelete.get(i) == true) {
                            Log.e(this.getClass().getSimpleName(), "item==>" + i);
                            new RentBarDao(context).delete(modelForDaos.get(i).get_id());
                            isTrue = true;
                        }
                    }
                    if (isTrue) {
                        Toast.makeText(context, R.string.delete_success, Toast.LENGTH_LONG).show();
                        itemSelete.put(0,false);
                        loadData(null);
                    }else {
                        Toast.makeText(context, R.string.delete_choose, Toast.LENGTH_LONG).show();
                    }
                    if (new RentBarDao(context).getAllValid().size() == 0) {
                        rl_bottom_delete.setVisibility(View.GONE);
                    }
                }else {
                    Toast.makeText(context, R.string.delete_choose, Toast.LENGTH_LONG).show();
                }
                break;

            case R.id.ll_choose_all:
                if (isItemAllClick) {
                    for (int i = 0; i < modelForDaos.size(); i++) {
                        itemSelete.put(i, true);
                    }
                    adapter.setClick(itemSelete);
                    iv_choose_all.setImageDrawable(getResources().getDrawable(R.drawable.btn_fx_selected));
                    isItemAllClick = false;
                } else {
                    for (int i = 0; i < modelForDaos.size(); i++) {
                        itemSelete.put(i, false);
                    }
                    adapter.setClick(itemSelete);
                    iv_choose_all.setImageDrawable(getResources().getDrawable(R.drawable.btn_fx_unselected));
                    isItemAllClick = true;
                }
                break;
        }
    }

    public void deteleItem(boolean isVisible) {
        Log.e(this.getClass().getSimpleName(), "isVisible==>" + isVisible);
        if (modelForDaos.size() > 0) {
            itemSelete.clear();
            adapter.setClick(itemSelete);
            adapter.setVisible(isVisible);
            if (isVisible) {
                rl_bottom_delete.setVisibility(View.VISIBLE);
            } else {
                rl_bottom_delete.setVisibility(View.GONE);
            }
            iv_choose_all.setImageDrawable(getResources().getDrawable(R.drawable.btn_fx_unselected));
        } else {
            rl_bottom_delete.setVisibility(View.GONE);
        }
    }

    @Override
    public void payClick(View v) {
        itemPosition = (int) v.getTag();
        switch (v.getId()) {
            case R.id.pay_now:
                orderImpl.submitOrder(modelForDaos.get(itemPosition).getFrameColorCode(), modelForDaos.get(itemPosition).getRefractiveId()
                        , modelForDaos.get(itemPosition).getProductNo());
                break;
            case R.id.iv_select:
                if (isItemClick) {
                    itemSelete.put(itemPosition, isItemClick);
                    adapter.setClick(itemSelete);
                    isItemClick = false;
                } else {
                    itemSelete.put(itemPosition, isItemClick);
                    adapter.setClick(itemSelete);
                    isItemClick = true;
                }
                break;
        }

    }

    @Override
    public void onPaySuccess(String resultStatus) {
        deleteItem(itemPosition);
        Bundle bundle = new Bundle();
        bundle.putInt("orderId", orderId);
        ActivityUtil.setIntent(context, ConfirmInfoActivity.class, bundle);
    }

    @Override
    public void onPayFail() {

    }

    //----------------------------------------------------------------------
    @Override
    public void onSubmitSuccess(OrderSubmit msg) {
        orderId = msg.getId();
        new DialogPay(context, msg.getOrderNo(), this);
    }

    @Override
    public void onSubmitFail() {

    }

    @Override
    public void oncompleteSuccess() {

    }

    @Override
    public void oncompleteFail() {

    }

    @Override
    public void onDeliverSuccess() {

    }

    @Override
    public void onDeliverFail() {

    }
    private void deleteItem(int position) {
        Toast.makeText(context, R.string.delete_success, Toast.LENGTH_LONG).show();
        new RentBarDao(context).delete(modelForDaos.get(position).get_id());
        loadData(null);
        itemSelete.clear();
        adapter.setClick(itemSelete);
    }

}
